package com.ll.gulimall.member.interceptor;

import com.ll.common.constant.AuthServerConstant;
import com.ll.common.vo.MemberEntityVo;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class OrderInterceptor implements HandlerInterceptor {
    public static ThreadLocal<MemberEntityVo> threadLocal = new ThreadLocal<>();


    /**
     * 目标方法前处理
     *
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String uri = request.getRequestURI();
        if (new AntPathMatcher().match("/order/order/orderStatus/**", uri)) {
            //无需登录的请求
            return true;
        }
        MemberEntityVo member = (MemberEntityVo) request.getSession().getAttribute(AuthServerConstant.LOGIN_USER);
        if (member != null) {
            //已登录 利用threadLocal 共享用户信息
            threadLocal.set(member);
            return true;
        } else {
            //未登录
            response.sendRedirect("http://auth.gulimall.com/login.html");
            return false;
        }

    }
}
